package fcl.mylife;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import fcl.platform.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LifeMapActivity extends MapActivity {

	private Projection projection;
	private Itinerary path = new Itinerary();
	private Button button1;
	MyItemizedOverlay itemizedoverlay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		MapController mapControl;

		setContentView(R.layout.mapview);

	
		
		// load google map
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setSatellite(false);
		mapView.setBuiltInZoomControls(true);
		mapControl = mapView.getController();
		mapControl.setZoom(10);
		


		// draw overlay item
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(
				R.drawable.none);
		
		itemizedoverlay = new MyItemizedOverlay(drawable, mapView);

		projection = mapView.getProjection();
		mapOverlays.add(new MyOverlay());
		
		
        mapOverlays.add(itemizedoverlay);

		// define a geopoint
		
   //define a geopoint
		
		//load the data
		if(path == null){
			path = new Itinerary();
			
		}

		path.requestdata(this.getBaseContext());
		path.reload();
		
		
		//attention here we fill the items
		fillOverlayItem();
		
		Stop center = path.getCenter();
		
        
		String coordinates[] = {"47.371266","8.539585"}; //Zurich
//		String coordinates[] = {"37.5021","126.981146"}; //Seoul
//		String coordinates[] = {"37.982092","23.729241"}; //Athens
//		String coordinates[] = {"1.288897","103.827912"}; //Singapore
		
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
        
		if(center!=null){
			lat = center.getX();
	        lng = center.getY();
		}else{
			  
		}
        MapController mc = mapView.getController();
      
      
 
        GeoPoint point = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
       
        mc.animateTo(point);

	

//		OverlayItem overlayitem = new OverlayItem(point, "hello",
//				"I'm in Singapore!");
//
//		itemizedoverlay.addOverlay(overlayitem);
//		mapOverlays.add(itemizedoverlay);

		// green circle
		// itemizedoverlay1.addOverlay(overlayitem2);
		// mapOverlays.add(itemizedoverlay1);
        
      //find id
        /* 
        button1 = (Button)findViewById(R.id.refreshbutton); 
        

        button1.setOnClickListener(new Button.OnClickListener(){ 
 
           

			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Toast.makeText(LifeMapActivity.this,
	                       "the trajectory will be updated", 
	                        Toast.LENGTH_LONG).show();
	              
	             this.refresh();
			}

			private void refresh() {
				// TODO Auto-generated method stub
				path.requestdata(null);
			}
        });
        */

	}

	private void fillOverlayItem() {
		
		
		ArrayList<Stop> ss = path.getStops();
		for(int i = 0; i < ss.size(); i++){
			// TODO Auto-generated method stub
			
			
	        double lat = ss.get(i).getX();
	        double lng = ss.get(i).getY();
	 
	        GeoPoint point = new GeoPoint(
	            (int) (lat * 1E6), 
	            (int) (lng * 1E6));
	       
	        //you can write extra information here~!
	        OverlayItem overlayitem = new OverlayItem(point, 
	        		"location type: "+ ss.get(i).getLocType(), 
	        		"Location: " + " x:" + ss.get(i).getX() + " y:" + ss.get(i).getY() +
	        		" Time: " + ss.get(i).getStime());
	        
	        itemizedoverlay.addOverlay(overlayitem);
		}
 
	}

	class MyOverlay extends Overlay {

		@Override
		public void draw(Canvas canvas, MapView mapv, boolean shadow) {
			super.draw(canvas, mapv, shadow);

			this.drawElements(mapv, canvas, "this");

			
			path.reload();
			
//			 Paint mPaint = new Paint();
//			 mPaint.setDither(true);
//			 mPaint.setColor(Color.BLUE);
//			 mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//			 mPaint.setStrokeJoin(Paint.Join.ROUND);
//			 mPaint.setStrokeCap(Paint.Cap.ROUND);
//			 mPaint.setStrokeWidth(3);
//			
//		//	 String coordinates[] = {"1.289478890541", "103.80733282853"};
//			 double lat = path.moves.get(0).getStart().getX();
//			 double lng = path.moves.get(0).getStart().getY();
//			
//			 GeoPoint gP1 = new GeoPoint(
//			 (int) (lat * 1E6),
//			 (int) (lng * 1E6));
//			
//			 String coordinates2[] = {"1.31776", "103.69006"};
//			 double lat2 = path.moves.get(0).getEnd().getX();
//			 double lng2 = path.moves.get(0).getEnd().getY();
//			
//			 GeoPoint gP2 = new GeoPoint(
//			 (int) (lat2 * 1E6),
//			 (int) (lng2 * 1E6));
//			
//			 //GeoPoint gP1 = new GeoPoint(19240000,-99120000);
//			 //GeoPoint gP2 = new GeoPoint(37423157, -122085008);
//			
//			 Point p1 = new Point();
//			 Point p2 = new Point();
//			
//			 Path path = new Path();
//			
//			 Projection projection = mapv.getProjection();
//			 projection.toPixels(gP1, p1);
//			 projection.toPixels(gP2, p2);
//			
//			 path.moveTo(p2.x, p2.y);
//			 path.lineTo(p1.x,p1.y);
//			
//			 canvas.drawPath(path, mPaint);
			
	
		}

		private void drawStop(MapView mapView, Canvas canvas, Stop s) {
			
			float circlesize = 0;  //init
			
			int zoom = mapView.getZoomLevel();
			
			circlesize = zoom  * (float)1.5;
			
			// TODO Auto-generated method stub
			double lat = s.getX();
			double lng = s.getY();

			GeoPoint gP1 = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

			Point screenPts = new Point();

			mapView.getProjection().toPixels(gP1, screenPts);

			// // ---add the marker---
			// Bitmap bmp = BitmapFactory.decodeResource(getResources(),
			// R.drawable.service_on);
			// canvas.drawBitmap(bmp, screenPts.x - 25, screenPts.y - 60,
			// null);
			//

			
			Paint mPaint = new Paint();
			mPaint.setDither(true);
		//	mPaint.setColor(Color.RED);
			mPaint.setStyle(Paint.Style.FILL);
			//mPaint.setStrokeJoin(Paint.Join.ROUND);
			//mPaint.setStrokeCap(Paint.Cap.ROUND);
			
			//i add
			//mPaint.setColor(Color.WHITE);
			//mPaint.setStrokeWidth(1);
			
            int type = s.getLocType();
			
			if(type == 0){ //HOME
				mPaint.setARGB(50, 50, 205, 50); //green
			}
			
			else if (type == 1){//WORKING PLACE
				mPaint.setARGB(50, 0,191,255); //blue
			}
			
			else if(type == 2){ //WEEKEND
				mPaint.setARGB(50, 255,69,0); //orange
				
			}else if (type != 1 && type != 2 && type != 0) {
				mPaint.setARGB(50, 255,69,0); //orange
			}
			 
			
			canvas.drawCircle(screenPts.x, screenPts.y, circlesize, mPaint);
			//Draw OUTLINE
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setStrokeWidth(2);
			mPaint.setARGB(100,255,255,255);
			canvas.drawCircle(screenPts.x, screenPts.y, circlesize, mPaint);
			
			
//			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
//			R.drawable.service_on);
		//	canvas.drawBitmap(bmp, screenPts.x - 15, screenPts.y -15, null);
		}

		private void drawLink(MapView mapView, Canvas canvas, Move m) {
			
			Stop s = m.getStart();
			Stop e = m.getEnd();
			
			// TODO Auto-generated method stub
			double lat1 = s.getX();
			double lng1 = s.getY();

			GeoPoint gP1 = new GeoPoint((int) (lat1 * 1E6), (int) (lng1 * 1E6));

			Point screenPts1 = new Point();

			mapView.getProjection().toPixels(gP1, screenPts1);

			double lat2 = e.getX();
			double lng2 = e.getY();

			GeoPoint gP2 = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6));

			Point screenPts2 = new Point();
			mapView.getProjection().toPixels(gP2, screenPts2);

			Paint mPaint = new Paint();
			//mPaint.setDither(true);
			
			int type = m.getVehicle();
			
			if(type == 1){
				mPaint.setARGB(30,50,205,50);
			}else if (type == 2){
				mPaint.setARGB(30, 255,69,0);
			}else if(type == 3){
				mPaint.setARGB(30, 30,144,255);
			}else if((type == 4)||(type ==5)){
				mPaint.setARGB(30, 190,190,190);
			}
			
			mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
			//mPaint.setStrokeJoin(Paint.Join.ROUND);
			//mPaint.setStrokeCap(Paint.Cap.ROUND);
			mPaint.setStrokeWidth(5);  //line size

			Path path = new Path();

			path.moveTo(screenPts1.x, screenPts1.y);
			path.lineTo(screenPts2.x, screenPts2.y);

			canvas.drawPath(path, mPaint);
			
			
		}

		public void drawElements(MapView mapView, Canvas canvas, String element) {

			if (mapView == null) {
				return;
			}

			// Get elements
			path.reload();

			// now we draw the links

			ArrayList<Move> moves = path.getMoves();

			if (moves != null && moves.size() != 0) {
				for (Move m : moves) {

					
					drawLink(mapView, canvas, m);
					drawStop(mapView, canvas, m.getStart());
					drawStop(mapView, canvas, m.getEnd());
				}
			}
			

		}

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}